package com.xique.park.service.command.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.command.CommandResultUtil;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.api.IParkLaneCommandApi;
import com.xique.park.bean.domain.ParkLane;
import com.xique.park.constant.CommandConstant;
import com.xique.park.constant.ParkApiName;
import com.xique.park.service.command.service.IParkLaneCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 通道命令下发service
 *
 * @author: xique
 */
@Service
public class ParkLaneCommandServiceImpl extends CommandCommServiceImpl implements IParkLaneCommandService {

    @Autowired
    private IParkLaneCommandApi parkLaneCommandApi;

    @Override
    public AjaxResult downParkLane(ParkLane parkLane, String reminderMsg) {
        Long parkId = SecurityUtils.getParkId();
        String deviceCode = getMainDeviceCodeByLaneId(parkId, parkLane.getId());
        // 不存在主设备
        if (StrUtil.isBlank(deviceCode)) {
            return AjaxResult.success(reminderMsg + "未配置主相机，无需下发");
        }
        JSONObject obj = getBaseObj(SecurityUtils.getParkId(), getMainDeviceCodeByLaneId(parkId, parkLane.getId()), ParkApiName.COMMAND_SET_LANE_PARAM, ParkApiName.VER_2_0);
        obj.put(CommandConstant.DATA, JSONObject.toJSON(parkLane));
        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);
        AjaxResult result = parkLaneCommandApi.downParkLane(params);
        return CommandResultUtil.handleCommandResult(result, reminderMsg);
    }

    @Override
    public AjaxResult downParkLane(Long parkId, String sn, ParkLane parkLane) {
        JSONObject obj = getBaseObj(parkId, sn, ParkApiName.COMMAND_SET_LANE_PARAM, ParkApiName.VER_2_0);
        obj.put(CommandConstant.DATA, JSONObject.toJSON(parkLane));
        @SuppressWarnings("unchecked")
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);
        AjaxResult result = parkLaneCommandApi.downParkLane(params);
        return CommandResultUtil.handleCommandResult(result, "");
    }
}
